import { _decorator, AudioClip, AudioSource, Component, director, Node } from 'cc';
import { ResManager } from '../res/ResManager';
const { ccclass, property } = _decorator;

/** 单例模式的音频管理器 */
export class AudioManager {
    private static _instance: AudioManager = null!;
    /** AudioSource 挂载在此节点上 */
    private m_AttachNode: Node = null;
    /** AudioSource 组件 */
    private m_AudioSource: AudioSource = null;
    /** 获取单例的接口 */
    static getInstance() {
        if (this._instance === null) {
            this._instance = new AudioManager();
        }
        return this._instance;
    }

    private constructor() {
        // 私有化的构造函数
         this.m_AttachNode = director.getScene().getChildByName("Canvas");
         this.m_AudioSource = this.m_AttachNode.addComponent(AudioSource);
    }

    // playMusic(bUrl:{
    //     /**子包名 */b:string,
    //     /**资源路径 */l:string
    // }):void{
    //     gtr.res.loadAudioClip(bUrl.b,bUrl.l,audioClip  => {
    //         let audioSource = this.m_AudioSource;
    //         audioSource.clip = audioClip;
    //         audioSource.loop = true;
    //         audioSource.play();
    //     })
    // }

    async playMusic(bUrl:IBundleUrl){

        //加载背景后播放
        const audioClip = await gtr.res.loadAssetAsync(bUrl,AudioClip);
        let audioSource = this.m_AudioSource;
        audioSource.clip = audioClip;
        audioSource.loop = true;
        audioSource.play();

    }
}


